package com.wufun.overseas.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;

/**
 * Created by xiet on 2016/5/6.
 */
public class XListView4NewGame extends XListView2 {
    public final static String TAG = "XListView4NewGame";

    public XListView4NewGame(Context context) {
        super(context);
    }

    public XListView4NewGame(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public XListView4NewGame(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private int mLastIntercept = 0;

    private GestureDetector mGestureDetector = new GestureDetector(new YScrollDetector());

    class YScrollDetector extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                float distanceX, float distanceY) {
            return Math.abs(distanceY) > 1.5f * Math.abs(distanceX);

        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {


        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:
                final float deltaY = ev.getRawY() - mLastY;
                mLastY = ev.getRawY();
                final float deltaX = ev.getRawX() - mLastX;
                mLastX = ev.getRawX();

                if (!mGestureDetector.onTouchEvent(ev)) {
                    return false;
                }

//                if (Math.abs(deltaX) > Math.abs(deltaY)) return false;

                break;
            case MotionEvent.ACTION_DOWN:
                break;
        }
        return super.onTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        if (!mGestureDetector.onTouchEvent(ev)) {

//            Logger.log(TAG, "onInterceptTouchEvent", "false");

            if (mLastIntercept != 0) {
                return false;
            }
            mLastIntercept = 1;

            if (ev.getAction() == MotionEvent.ACTION_UP) {
                mLastIntercept = 0;
            }
        }

//        Logger.log(TAG, "onInterceptTouchEvent", "true");
        mLastIntercept = 0;
        return super.onInterceptTouchEvent(ev);
    }


//    public static final int DEVIATION = 5;
//    //偏移量
//    private float dx = 0;
//    //是否拦截事件
//    private boolean isAllowIntercept = false;
//
//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        switch (ev.getAction()) {
//
//            case MotionEvent.ACTION_DOWN:
//                dx = 0;
//                isAllowIntercept = false;
//                break;
//
//            case MotionEvent.ACTION_MOVE:
//                if(dx != 0 ){
//                    dx = dx - ev.getX();
//
//                }
//
//                if(Math.abs(dx) > DEVIATION){
//                    isAllowIntercept = true;
//                }
//
//                requestDisallowInterceptTouchEvent(isAllowIntercept);
//
//                dx = ev.getX();
//
//                break;
//
//            case MotionEvent.ACTION_UP:
//                dx = 0;
//                isAllowIntercept = false;
//                break;
//
//            default:
//                break;
//
//
//        }
//
//
//
//        return super.dispatchTouchEvent(ev);
//    }
}
